$(function() {
	// 加载菜单
	$.post(frame.fullUrl("/base/menu/query.json"), function(data) {
		if (data) {
			$('#index_menu').tree("loadData", data);
			$('#index_menu').tree("collapseAll");
			bodyResize();
			
			var homePage = frame.data.getValue("homePage");
			if(homePage != null ){
				var node = $('#index_menu').tree("find", homePage);
				if(node != null)
					index_newtab(node);
			}
		}
	});
	$('#index_menu').tree({
		onClick : index_newtab
	});
	$("#easylayout").find(".panel-title").css({"font-size":14,"margin":4});
	menuPanel = $("#easylayout").find(".layout-panel-west");
	$(window).resize(function() {
		if (this.resizeTO) clearTimeout(this.resizeTO);
		this.resizeTO = setTimeout(function() {
			bodyResize();
		}, 300);
	});
});

var menuPanel = null;
var isBigWin = null;
function bodyResize(){
	var winWidth = $(window).width();
	isBigWinT = false;
	if(winWidth>800){
		isBigWinT = true
	}
	if (isBigWin == null || isBigWin != isBigWinT) {
		if (isBigWinT) {
			menuPanel.find(".panel-tool").show();
			$('#index_tabs').tabs({
				tools : null
			});
			menuPanel.show();
			$("#easylayout").layout("resize");
		} else {
			$("#easylayout").layout("expand", "west");
			menuPanel.find(".panel-tool").hide();
			menuPanel.hide();
			$('#index_tabs').tabs({
				tools : [ {
					iconCls : 'icon-add',
					handler : showMenu
				} ]
			});
			$("#easylayout").layout("resize");
		}
		isBigWin = isBigWinT;
	} else if(isBigWin == false && isBigWinT == false){
		menuPanel.hide();
		$("#easylayout").layout("panel", "west").panel("resize",{
			height: $(window).height()
		});
		$("#easylayout").layout("resize");
	}
	return true;
}

function showMenu(){
	menuPanel.slideToggle();
}

/**
 * 点击菜单响应
 * 
 * @param node
 */
function index_newtab(node) {
	var isleaf = $('#index_menu').tree("isLeaf", node.target);
	if (isleaf) {
		if(!isBigWin) showMenu();
		if (!node.attributes.url) {
			$.messager.show({
				title : '提示',
				msg : "菜单URL为空！",
				timeout : 2000,
				showType : 'show'
			});
			return;
		}
		var id = "tab_" + node.id.replace("menu_", "");
		var tab = $("#" + id);
		if (tab.length > 0) {
			var index = $("#index_tabs").tabs('getTabIndex', tab);
			$("#index_tabs").tabs("select", index);
		} else {
			//iframe的src属性先为空，然后再赋值，解决iframe下页面请求两遍问题
			//http://fruithardcandy.iteye.com/blog/1884749
			$('#index_tabs').tabs('add',{
					id : id,
					title : node.text,
					content : "<iframe id=" + node.text
							+ " src='"+frame.fullUrl(node.attributes.url)+"'></iframe>",
					closable : true,
					fit : true
				});
		}
	} else {
		if (node.state == "closed") {
			$('#index_menu').tree("expand", node.target);
		} else {
			$('#index_menu').tree("collapse", node.target);
		}
	}
}

function index_closetab(id) {
	var tabs = $('#index_tabs').tabs('close', id);
}

function changeTheme(value) {
	if(value){
		frame.data.saveData({THEME:value},function(){
				window.location.href=window.location.href; 
				window.location.reload; 
			});
	}
}

function logout(){
	$.post(frame.fullUrl("/base/login/logout.json"), function() {
			window.location.href=window.location.href; 
			window.location.reload(); 
	});
}

function changeAcount(value){
	if(value){
		$.post(frame.fullUrl("/base/loginuser/changeacount.json"),
			{acountId:value},
			function(data) {
				var tabs = $('#index_tabs').tabs("tabs");
				for(var i=tabs.length-1;i>0;i--)
					$('#index_tabs').tabs("close", i);
					$.messager.show({
						title : '提示',
						msg : "切换账户成功！",
						timeout : 1000,
						showType : 'show',
						style:{
							right:'',
							bottom:''
						}
					});
		});
	}
}

function click_hide_north(){
	$("#index_top").slideToggle(function(){
		$("#easylayout").layout("resize");
	});
}